package com.kushe.site.controller;

import com.kushe.dao.MessageDao;
import com.kushe.dao.NoticeDao;
import com.kushe.entity.Notice;
import com.kushe.entity.User;
import com.kushe.util.KSController;
import com.kushe.util.SessionEnv;
import com.kushe.util.core.DaoFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by WhiteBlue on 15/5/21.
 */
@org.springframework.stereotype.Controller
@RequestMapping(value = "/notice")
public class NoticeController extends KSController {

    @RequestMapping(value = "", method = {RequestMethod.HEAD, RequestMethod.GET})
    public Object index(@RequestParam(value = "notice", required = true) int notice_id, HttpServletRequest request, HttpServletResponse response) {
        this.clearAttr(request);
        Notice notice = DaoFactory.getDao(NoticeDao.class).setRead(notice_id);
        User user = (User) request.getSession().getAttribute("user");

        SessionEnv sessionEnv = new SessionEnv();
        NoticeDao noticeDao = DaoFactory.getDao(NoticeDao.class);
        MessageDao messageDao = DaoFactory.getDao(MessageDao.class);

        sessionEnv.setList_message(messageDao.listNew(user.getId()));
        sessionEnv.setList_notice(noticeDao.listMessageNew(user.getId()));
        sessionEnv.setList_order(noticeDao.listOrderNew(user.getId()));

        request.getSession().removeAttribute("sessionEnv");
        request.getSession().setAttribute("sessionEnv", sessionEnv);

        if (notice != null) {
            return this.redirect("/design/info?design=" + notice.getDesign().getId());
        } else {
            return this.renderError(404, response);
        }
    }


}
